package com.scalar.chiptrack.operations.dataentry.inventoryTransit.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.inventoryTransit.forms.InventoryTransitForm;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.release.dao.RelaseDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;


/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 19 Feb, 2010
 * Time: 12:03:11 PM
 */
public class InventoryOutTransactionLoadLotNumbersCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/inventoryOutTrans.do";

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

        InventoryTransitForm inventoryTransitForm = (InventoryTransitForm) form;

        HashMap<String,String> errors   = new HashMap<String,String>();
        HashMap<String,String> info     = new HashMap<String,String>();
        String action = request.getParameter("action");
        String locationType = null;
        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
        ArrayList allRunningWipLotList;

        ArrayList vendorList = new ArrayList();
        
       try
       {
            locationType = NewProductDAO.getBaseProcessName( null, inventoryTransitForm.getProcessType() );

            inventoryTransitForm.setBaseprocessType( locationType );
           
            if ( user.getVendorId() > 0 ) // for VENDOR INTERFACE
            {
                inventoryTransitForm.setLocationId(0);
                if (inventoryTransitForm.getLocationId() == 0 )
                {
                    vendorList =  VendorDAO.getInventoryLocationsByProcessByUserType( null, locationType , user.getVendorId() );
                    if ( vendorList != null && vendorList.size() > 0  )
                    {
                        inventoryTransitForm.setLocationId( Integer.parseInt ( ( (LabelValueBean)vendorList.get(0) ).getValue() ) );
                    }
                }
            }
            if ( locationType.equalsIgnoreCase( LOCATION_TYPE_FAB ) || locationType.equalsIgnoreCase( LOCATION_TYPE_WAFER_STORE )
                    || locationType.equalsIgnoreCase( LOCATION_TYPE_SORT ) || "Back Grind".equalsIgnoreCase(inventoryTransitForm.getProcessType() )
                    || locationType.equalsIgnoreCase( LOCATION_TYPE_DIE_BANK )  )
            {
                 request.setAttribute("showWaferIds", "TRUE");
            }
            else
            {
                request.setAttribute("showWaferIds", "FALSE");
            }
            if (locationType.equalsIgnoreCase( LOCATION_TYPE_DIE_BANK ) )
            {
                allRunningWipLotList = DataEntryDAO.loadAllDieBankWipLots(inventoryTransitForm.getProcessStepId(), inventoryTransitForm.getLocationId(), inventoryTransitForm.getLotId(), false ,false  );
            }
            else
            {
                allRunningWipLotList = DataEntryDAO.getAllRunningHoldLots( inventoryTransitForm.getProcessType() , inventoryTransitForm.getProcessStepId() , inventoryTransitForm.getLocationId(), inventoryTransitForm.getLotId()  );
            }

            if ( allRunningWipLotList == null || allRunningWipLotList.size() == 0 )
            {
                info.put("No " + inventoryTransitForm.getProcessType() + " WIP lots available" , null );
            }

            inventoryTransitForm.setWipLotsList( allRunningWipLotList );

            request.setAttribute("lotsList", allRunningWipLotList );
            request.setAttribute("nextLocations",  RelaseDAO.loadProcessVendors( locationType ) );
            request.setAttribute("reciveProcessList", NewProductDAO.loadAllNextPsTypes(null, inventoryTransitForm.getProcessType(), true ) );
            request.setAttribute("processTypeList", NewProductDAO.loadAllProcessesFromDB(null, false) );
            request.setAttribute("partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( inventoryTransitForm.getProcessType() ) );
            request.setAttribute("locationList", VendorDAO.getInventoryLocationsByProcessByUserType( null, locationType, user.getVendorId() ) );

            request.setAttribute("action", action);
        }
        catch( Exception e)
        {
           request.setAttribute("processTypeList", NewProductDAO.loadAllProcessesFromDB(null, false)  );
           errors.put( "Unable to get Inventory Lots", null);
           e.printStackTrace();
        }
        return mapping.findForward( "inventoryOutTrans" );
    }
}
